package com.tiut.lmap.tools;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.tiut.lmap.model.pojo.Course;
import com.tiut.lmap.model.pojo.CourseReservation;
import com.tiut.lmap.model.pojo.School;
import com.tiut.lmap.model.query.CourseQuery;
import com.tiut.lmap.service.CourseReservationService;
import com.tiut.lmap.service.CourseService;
import com.tiut.lmap.service.SchoolService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@RequiredArgsConstructor
@Component
public class CourseTools {
    @Autowired
    private CourseService courseService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private CourseReservationService courseReservationService;

    @Tool(description = "根据条件查询课程")
    public List<Course> queryCourse(@ToolParam(required = false, description = "课程查询条件") CourseQuery query) {
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        QueryWrapper<Course> wrapper = queryWrapper.eq("type", query.getType())
                .le("edu", query.getEdu());
        if (query.getSorts() != null) {
            for (CourseQuery.Sort sort : query.getSorts()) {
                wrapper.orderBy(true, sort.getAsc(), sort.getField());
            }
        }
        List<Course> list = courseService.list(wrapper);
        return list;
    }

    @Tool(description = "查询所有校区")
    public List<School> queryAllSchools() {
        return schoolService.list();
    }

    @Tool(description = "生成课程预约单,并返回生成的预约单号")
    public String generateCourseReservation(
            String courseName, String studentName, String contactInfo, String school, String remark) {
        CourseReservation courseReservation = new CourseReservation();
        courseReservation.setCourse(courseName);
        courseReservation.setStudentName(studentName);
        courseReservation.setContactInfo(contactInfo);
        courseReservation.setSchool(school);
        courseReservation.setRemark(remark);
        courseReservationService.save(courseReservation);
        return String.valueOf(courseReservation.getId());
    }
}
